package com.rs.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.entity.Response;
import com.rs.entity.Term;
import com.rs.entity.UserVo;
import com.rs.entity.excel.StudentExcelEntity;
import com.rs.entity.login.Role;
import com.rs.entity.login.User;
import com.rs.service.RoleService;
import com.rs.service.TermService;
import com.rs.service.UserService;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author dehang
 * @date 2019/06/01
 */
@Controller
@RequestMapping("term")
public class TermController {

    @Autowired
    private TermService termService;

    @RequestMapping("/index")
    public String index(Model model) {
        return "term/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Response list(Integer page, Integer limit, String name) {
        Page<Object> objectPage = PageHelper.startPage(page, limit);
        List<Term> terms = termService.list(name);
        return Response.list(terms, objectPage.getTotal());
    }

    @RequestMapping("/getByKey/{id}")
    @ResponseBody
    public Response<Term> getByKey(@PathVariable(value = "id") Integer id) {
        Term term = termService.getByKey(id);
        return Response.success(term);
    }

    @RequestMapping("/save")
    @ResponseBody
    public int save(@RequestParam(required = false) Integer id, String name,String no){
        return termService.save(id, name,no);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Boolean del(@RequestBody List<Integer> ids) {
        return termService.delete(ids);
    }


    @RequestMapping("/uploadStudents")
    @ResponseBody
    public Response<Boolean> test(@RequestParam("file") MultipartFile multipartFile, @RequestParam("termId") Integer termId) throws Exception {
        ImportParams params = new ImportParams();
//       params.setTitleRows(0);
//       params.setHeadRows(0);
        long start = System.currentTimeMillis();
        List<StudentExcelEntity> list = ExcelImportUtil.importExcel(multipartFile.getInputStream(), StudentExcelEntity.class, params);

        termService.saveStudents(termId, list);
        return Response.success(Boolean.TRUE);
    }

}
